package com.magicsoft.app.helper;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.adapter.SelectorDialogAdapter;
import com.magicsoft.weitown.ui.SelectorDialog;

public class ImageDialogHelper {
	boolean flag;// 是否相册选择图片
	private Context aContext;
	private String aPath;
	private String savePath = "";
	// 是否进入相册或者打开相机拍照
	private boolean isSelectPhoto = false;
	// 是否跳到后台去剪切照片
	private boolean isCropPhoto = false;

	public String getSavePath() {
		return savePath;
	}

	public ImageDialogHelper(Context context, String _path) {
		aContext = context;
		aPath = _path;
	}

	/**
	 * 选择上传图片来源
	 */
	public void popupSectionDialog() {

		final SelectorDialog dialog = new SelectorDialog(aContext,
				R.style.selectorDialog);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();

		// 添加选项名称
		ArrayList<String> datas = new ArrayList<String>();
		// 拍照
		datas.add(aContext.getResources().getString(R.string.photograph));
		// 从相册选择
		datas.add(aContext.getResources().getString(R.string.photo_choosertask));
		// 取消
		datas.add(aContext.getResources().getString(R.string.vision_cancel));

		ListView listView = (ListView) dialog.findViewById(R.id.listview);

		SelectorDialogAdapter adapter = new SelectorDialogAdapter(aContext,
				datas);

		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:// 拍照
					startCameraActivity();
					isSelectPhoto = true;
					dialog.dismiss();
					break;
				case 1:// 从相册选择
					startImagePickActivity();
					isSelectPhoto = true;
					dialog.dismiss();
					break;
				case 2:
					// 取消
					dialog.dismiss();
					break;
				}
			}
		});
	}

	/**
	 * 选择相册图片
	 */
	private void startImagePickActivity() {

		flag = true;

		Intent i = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

		((Activity) aContext).startActivityForResult(i, 2);
	}

	/**
	 * 启动相机
	 */
	protected void startCameraActivity() {
		flag = false;

		File file = new File(aPath);
		Uri outputFileUri = Uri.fromFile(file);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (Exception e) {
				Log.i(aContext + "", "write image file failed");
			}

		}

		Intent intent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

		((Activity) aContext).startActivityForResult(intent, 2);

	}

	// 截取图片
	public void cropImage(Uri uri, int outputX, int outputY, int requestCode) {

		isCropPhoto = true;

		// 裁剪图片意图
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		// 裁剪框的比例，1：1
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// 裁剪后输出图片的尺寸大小
		intent.putExtra("outputX", outputX);
		intent.putExtra("outputY", outputY);
		// 图片格式
		intent.putExtra("outputFormat", "JPEG");
		intent.putExtra("noFaceDetection", true);
		intent.putExtra("return-data", true);
		((Activity) aContext).startActivityForResult(intent, requestCode);
	}

	/**
	 * 从相册选取图片
	 * 
	 * @param data
	 */
	public Bitmap onImagePicked(Cursor cursor, String[] filePathColumn) {
		// String[] filePathColumn = { MediaStore.Images.Media.DATA };
		cursor.moveToFirst();

		int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		String image_path = cursor.getString(columnIndex);// 相册图片路径
		cursor.close();

		Bitmap bitmap = null;

		if (StringUtils.isImageUrl(image_path)) {

			// BitmapFactory.Options options = new BitmapFactory.Options();
			// options.inSampleSize = 4;
			// Log.i("ImageDialogHelper", "imagewidth = " + options.outWidth +
			// " imageheight = " + options.outHeight);
			// Bitmap bitmap = BitmapFactory.decodeFile(_path, options);
			bitmap = BitmapHelper.getSmallBitmap(image_path);
		}

		if (bitmap != null) {
			savePath = FileUtil.saveBitmap2file(bitmap, aPath);
		}

		// if (bitmap != null) {
		// Bitmap roundBitmap = BitmapHelper.getRoundedCornerBitmap(bitmap);
		// return roundBitmap;
		// }

		// cardImage.setImageBitmap(bitmap);

		return bitmap;
	}

	/**
	 * 拍照获取图片
	 */
	public Bitmap sendImage() {

		// BitmapFactory.Options options = new BitmapFactory.Options();
		// options.inSampleSize = 4;
		//
		// Bitmap bitmap = BitmapFactory.decodeFile(aPath, options);
		Bitmap bitmap = BitmapHelper.getSmallBitmap(aPath);
		if (bitmap != null) {
			Bitmap roundBitmap = BitmapHelper.getRoundedCornerBitmap(bitmap,
					false);
			savePath = FileUtil.saveBitmap2file(bitmap, aPath);
			return roundBitmap;
		}
		return null;

	}

	public boolean isAlbum() {
		return flag;
	}

	public boolean isSelectPhoto() {
		return isSelectPhoto;
	}

	public void setSelectPhoto(boolean isSelectPhoto) {
		this.isSelectPhoto = isSelectPhoto;
	}

	public boolean isCropPhoto() {
		return isCropPhoto;
	}

	public void setCropPhoto(boolean isCropPhoto) {
		this.isCropPhoto = isCropPhoto;
	}

}
